********************************************************************************
*** Voting for Populism in Europe Replication Files
*** Figure 2: Manufacturing Imports 1990 - 2018
*** 
*** Required data files: "data/oecd_trade/FRED_deflator_2020 (2015 base).csv", "data/oecd_trade/oecd_chinese_trade_2020", "data/oecd_trade/oecd_lowwage.dta", and "data/econ_data/T-pedia/ardeco_regional_gdp.dta"
*** 
*** Note: Need to add ccode ado file from http://www.columbia.edu/~rh2883/stats.html
*** Created by: Thomas Cunningham
*** Date: 22 December 2020
******************************************************************
clear all
version 16.1
cd ..

**************************************
* 2015 USD constant dollars deflator (Accessed 24 May 2020)
* Source: https://fred.stlouisfed.org/series/USAGDPDEFQISMEI
**************************************

import delimited using "data/oecd_trade/FRED_deflator_2020 (2015 base).csv", varnames(1) clear
rename usagdpdefqismei usd_defl_2015
rename date year
sort year

tempfile defl
save `defl'

use "data/oecd_trade/oecd_chinese_trade.dta", clear

keep cname ccode year china_imp_D10T32 china_imp_DTOTAL

merge 1:1 ccode year using "data/oecd_trade/oecd_lowwage_trade.dta", keepusing(lwage_indus_imp lwage_total_imp)

keep if _merge==3

drop _merge

gen tag=0

replace tag=1 if inlist(cname, "Austria", "Belgium", "Denmark", "Finland", "France", "Germany", "Greece", "Ireland", "Italy")

replace tag=1 if inlist(cname, "Netherlands", "Norway", "Portugal", "Spain", "Sweden", "United Kingdom")

keep if tag==1

drop tag

preserve 

use "data/ardeco/ardeco_regional_gdp.dta", clear
keep if nutslevel==0

ccode nuts_name,from(CTY) to(COW) gen(ccode)

drop if ccode==.

gen tag=0

replace tag=1 if inlist(nuts_name, "Austria", "Belgium", "Denmark", "Finland", "France", "Germany", "Greece", "Ireland", "Italy")

replace tag=1 if inlist(nuts_name, "Netherlands", "Norway", "Portugal", "Spain", "Sweden", "United Kingdom")

keep if tag==1

drop tag

** Merge and Deflate to Constant 2015 Euros
merge m:1 year using `defl', gen(mdef)
	drop if mdef==2
	drop mdef

	
foreach v of varlist *_gdp { 
	local lbl : var label `v'
	gen `v'_c = `v'*1000000
	replace `v'_c = ((`v'_c/usd_defl_2015)*100) // convert to 2015 dollars
	gen euro_`v' = (`v'_c/1.1096)  // convert to 2015 Euros (FRED DEXUSEU)
	replace `v'_c = (euro_`v'/1000)
	la var `v'_c "`lbl' (2015 EUROS)"
}

tempfile gdp
save `gdp'

restore

merge 1:1 ccode year using `gdp'

drop if year==2019

drop if cname==""

drop _merge

keep cname year china_imp_D10T32 china_imp_DTOTAL lwage_indus_imp lwage_total_imp regional_gdp

foreach var of varlist china_imp_D10T32 china_imp_DTOTAL lwage_indus_imp lwage_total_imp{
	gen `var'_gdp=`var'/regional_gdp
}

keep year *_gdp
drop regional_gdp

sort year

collapse(mean) *_gdp, by(year)

tsset year

   

rename china_imp_D10T32_gdp cn_mfg_gdp
rename china_imp_DTOTAL_gdp cn_total_gdp
rename lwage_indus_imp_gdp lowwage_mfg_gdp
rename lwage_total_imp_gdp lowwage_total_gdp

foreach var of varlist *_gdp{
	replace `var'=`var'*100
	gen ma_`var'=(F1.`var'+`var'+L1.`var')/3
	drop `var'
	renvars ma_`var', predrop(3)
}

la var cn_mfg_gdp "Chinese Imports"
la var lowwage_mfg_gdp "Low-Wage Country Imports"

twoway line cn_mfg_gdp year|| line lowwage_mfg_gdp year, ///
 scheme(s1color) ///
text(2 2007.92 "Financial Crisis", place(e) size(small)) ///
xline(2008,lpattern(solid) lcolor(gray)) ///
xlab(1990(5)2020) ///
xtitle("") ///
ytitle("Mfg. Imports as a % of GDP") ///
title("Manufacturing Imports, 1990-2018")  ///
subtitle("15 European Countries") ///
note("Source: ARDECO, 2020; OECD 2020.", size(vsmall))
graph export "output/figure2.pdf", replace 



